package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.codehaus.plexus.util.StringUtils;
import zhang.lao.build.kit.LogKit;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.BeanMapTool;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnMachineToolDao;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.dao.base.WnMachineToolLinkDao;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.service.sequence.SequenceTool;

/**
* 
* @author 
* @create 
**/
 @Service
public class WnMachineToolService{
	@Resource
	private WnMachineToolDao wnMachineToolDao;
	@Resource
	private WnMachineToolLinkDao wnMachineToolLinkDao;
	@Resource
	private WnFarmDao wnFarmDao;
	@Resource
	private SequenceTool sequenceTool;
	public String add(){
		return "console/wnMachineTool/wnMachineTool_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnMachineTool", wnMachineToolDao.selectByPrimaryKey(id));
		return "console/wnMachineTool/wnMachineTool_form";
	}

	public String list(){
		return "console/wnMachineTool/wnMachineTool_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnMachineToolExample wnMachineToolExample = new WnMachineToolExample();
        ControllerQueryTool.setWnMachineToolCriteria(bootGridReq.getQuery(),wnMachineToolExample.createCriteria()).andFarmIdIn(consoleContext.getFarms());
		List<WnMachineTool> wnMachineToolList = wnMachineToolDao.selectByExample(wnMachineToolExample);
		List<JSONObject> list = BeanMapTool.objectsToJsonObjects(wnMachineToolList);
		if(list!=null){
			for (JSONObject jsonObject : list) {
				jsonObject.put("farmName",wnFarmDao.selectByPrimaryKey(jsonObject.getString("farmId")).getName());
			}
		}
			return new BootStrapGridResp(page.getTotal(),list);
	}

	public HttpResult save(String formObjectJson,String machineId){
		WnMachineTool wnMachineTool= JSON.parseObject(formObjectJson,WnMachineTool.class);
			java.lang.String id=wnMachineTool.getId();
		if (id!=null) {

			wnMachineToolDao.updateByPrimaryKeySelective(wnMachineTool);
		}else{
			wnMachineTool.setId(UUIDTool.getUUID());
			wnMachineTool.setToolNumber(sequenceTool.getSequence("MACHINE_TOOL_NUMBER")+"");
			wnMachineToolDao.insertSelective(wnMachineTool);
			if(StringUtils.isNotBlank(machineId)){
				WnMachineToolLink wnMachineToolLink = new WnMachineToolLink();
				wnMachineToolLink.setId(UUIDTool.getUUID());
				wnMachineToolLink.setMachineToolId(wnMachineTool.getId());
				wnMachineToolLink.setMachineId(machineId);
				wnMachineToolLinkDao.insert(wnMachineToolLink);
			}
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			WnMachineToolLinkExample wnMachineToolLinkExample = new WnMachineToolLinkExample();
			wnMachineToolLinkExample.createCriteria().andMachineToolIdEqualTo(id);
			wnMachineToolLinkDao.deleteByExample(wnMachineToolLinkExample);
		wnMachineToolDao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess("删除成功(仅解除与农机关联)!");
	}
}
